package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.MD5.AppMD5Util;
import com.common.mybatis.Page;
import com.common.utils.base.UuidUtil;
import com.entity.AttachmentRelationship;
import com.entity.LoginTable;
import com.entity.UserIndustryRelations;
import com.entity.Users;
import com.repository.AttaRelationshipDao;
import com.repository.IndRelationsDao;
import com.repository.LoginDao;
import com.repository.UsersDao;
import com.service.base.BaseServiceImp;
@Service("UsersService")
public class UsersServiceImp extends BaseServiceImp<Users> implements UsersService{
	
	//用户
	@Autowired
	private UsersDao useDao;
	//附件关系
	@Autowired
	private AttaRelationshipDao fjgxDao;
	//行业关系
	@Autowired
	private IndRelationsDao  IndReDao;
	//登陆关系
	@Autowired
	private LoginDao loninDao;
	

	@Transactional//事务回滚
	@Override
	public void insterFj(Users o, String AttachmentId,UserIndustryRelations Uir,LoginTable login) {
		String UserId=UuidUtil.get32UUID();
		String fujianArry[];
		if(AttachmentId.contains(",")){
			fujianArry=AttachmentId.split(",");
		}else{
			fujianArry=new String[]{AttachmentId};
		}
		//添加附件关系
		if(AttachmentId!=null){
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(UserId);
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("Users");
				fjgxDao.insert(fujiangx);
			}
		}
		o.setUsersId(UserId);
		Uir.setUserIndustryRelationsID(UuidUtil.get32UUID());
		Uir.setUsersId(UserId);
		login.setLoginId(UuidUtil.get32UUID());
		login.setLoginPassword(AppMD5Util.MD5(login.getLoginPassword()));
		loninDao.insert(login);
		//行业
		IndReDao.insert(Uir);
		//用户
		o.setLoginId(login.getLoginId());
		useDao.insert(o);
	}

	
	@Transactional//事务回滚
	@Override
	public void updateAll(Users o, UserIndustryRelations Uri,
			String AttachmentId) {
		String fujianArry[] ;
		if(AttachmentId.contains(",")){
			fujianArry=AttachmentId.split(",");
		}else{
			fujianArry=new String[]{AttachmentId};
		}
		if(AttachmentId!=null){
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(o.getUsersId());
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("Users");
	
				System.out.println("AttachmentRelationship"+fujiangx);
				fjgxDao.insert(fujiangx);
			}
		}
		
		IndReDao.update(Uri);
		useDao.update(o);
	}

	
	@Override
	public List<Users> findByLogin(Users o) {
		return useDao.findByLogin(o);
	}


	@Override
	public Page<Users> findPageByClassID(Users o, Page<Users> page, String cid) {
		page.setResults(useDao.findPageByClassID(o, page, cid));
		return page;
	}

}
